
const { dataSource } = this.widgetObj._option
const env = this.pageObj.getVariables().find(x => x.name === 'environmentType')?.value || ''
// const api = 'http://192.168.1.100:9098/basics-main/system/equipmentObject/listDetailByEquipmentCode'
const api = `/${env}Basics/basics-main/system/equipmentObject/listDetailByEquipmentCode`
const params = { ...$event }

console.log('dataSource: => ', dataSource)
console.log('params: => ', params)

function isHasData(copyData) {
    while (copyData.length) {
        const item = copyData.pop();
        if (item.catch && item.catch.has(params.id)) {
            return true;
        }
    }
}
function formatData(dataSource, rows) {
    dataSource.forEach(item => {
        if (item.id === params.id) {
            rows.forEach(carema => {
                const { equipmentConfig } = carema
                const _carema = {
                    ...carema,
                    title: carema.equipmentName,
                }

                if (equipmentConfig && equipmentConfig?.gbVideoConfig) {
                    const { sipUser, videoChannelId, sipServerHost, sipServerPort } = equipmentConfig.gbVideoConfig
                    _carema.stream_path = `${sipUser}/${videoChannelId}`
                    _carema.channelId = videoChannelId
                    _carema.deviceId = sipUser
                    _carema.host = `${sipServerHost}:${sipServerPort}`
                }
                const index = item.children.findIndex(ev => ev.id === _carema.id)
                if(index > -1) {
                    item.children.splice(index, 1, _carema)
                }else {
                    item.children.push(_carema)
                }

            })
        } else if (item.children?.length) {
            formatData(item.children, rows)
        }
    })
}

const { searchValue = "" } = window.RecordPage;
if (params.show && !isHasData([...dataSource])) {
    // `${api}?equipmentCode=camera&areaId=&pageSize=1&pageNum=1`
    fetch(`${api}?areaId=${params.id}&equipmentTypeCode=camera&equipmentName=${searchValue}`).then(res => {
        if (res.ok === true) {
            return res.json()
        }
    }).then(res => {
        const { rows } = res.result
        console.log('vvvvvvvvvvvvvvvv', dataSource)
        formatData(dataSource, rows)
        this.widgetObj.setData([{ data: dataSource }])
        this.widgetObj.render()
    })
}
